/* 
	--------------------------------------------------------------
	lightbox_google_admin_categories.js 2011-09-22 tb@gambio
	Gambio GmbH
	http://www.gambio.de
	Copyright (c) 2011 Gambio GmbH
	Released under the GNU General Public License (Version 2)
	[http://www.gnu.org/licenses/gpl-2.0.html]
	--------------------------------------------------------------
*/
(function($){
    $.fn.lightbox_google_admin_categories = function(options){     
		
	var settings = {
		template: "lightbox_google_admin_categories.html",
		lightbox_width: "840",
		lightbox_height: ""
	};
	
	if(options)
	{
		$.extend(settings,options);
	}
      
	var self = this;

	var lightbox_package;
	var lightbox_package_loading_image;	
	var lightbox_package_shadow;      
	var lightbox_package_container;
	var lightbox_package_wrapper;  
	var lightbox_package_content;       
	var window_width;
	var window_height;
	var lightbox_width;
	var lightbox_height;

	var loading_image = $("<img></img>").attr("src", "../images/loading.gif");
	var loading_image_box = $("<div></div>").addClass("loading_image_box").append(loading_image);
      
	$(self).bind("click", function(){
		
		window_width = $(window).width();
		window_height = $(window).height();

		lightbox_package = $("<div></div>").addClass("lightbox_package google_categories_administration");
		lightbox_package_shadow = $("<div></div>").addClass("lightbox_package_shadow");
		lightbox_package.append(lightbox_package_shadow);		
		lightbox_package_loading_image = $("<img></img>").attr("src", "../images/loading.gif");
		lightbox_package_loading_image.css("position", "absolute").css("top", window_height/2-8).css("left", window_width/2-8);
		lightbox_package.append(lightbox_package_loading_image);
		$("body").append(lightbox_package);
		
		fillTemplate();

		$(document).bind("scroll", function(){
			update_window();
		});

		$(window).bind('resize', function() {
			update_window();
		});
		
		$(".google_category_save .save").die();
		$(".google_category_save .save").live("click", function(){
			if($(".google_category_list_add_box").css("display") == "none"){
				var t_categories_array = new Array();
				$.each($(".google_category_list input[type=text]"), function(key, value){
					t_categories_array.push($(value).val());
				})
				
				var t_recursive = "off";
				if($("input.google_recursive_mode:checked").length > 0){
					t_recursive = "on";
				}
				
				var t_overwrite = "off";
				if($("input.google_overwrite_categories:checked").length > 0){
					t_overwrite = "on";
				}

				$.ajax({
					type: "POST",
					url: "request_port.php?module=GoogleTaxonomy&action=save_category_google_categories",
					data: {"categories_id": $(self).attr("rel"), "google_recursive_mode": t_recursive, "google_overwrite_categories": t_overwrite, "category_list": t_categories_array},
					success: function(){
                        $(window).unbind("scroll");
                        $(window).unbind('resize');
                        $('body').css('overflow', 'auto');
						$(".lightbox_package").remove();
					}
				});
			}
			return false;
		});

		$(".google_category_list_add_button a").die();
		$(".google_category_list_add_button a").live("click", function(){
			$(".google_category_list_add_box").append(loading_image_box);
			$(".google_category_list_add_button a").hide();
			$(".google_category_save a").hide();
			show_category_add_box("");
			return false;
		});

		$(".google_category_list .edit").die();
		$(".google_category_list .edit").live("click", function(){
			$(".google_category_list input").removeClass("edit");
			$(this).prev().addClass("edit");
			var t_value = $(this).prev().val();
			$(".google_category_list_add_box").append(loading_image_box);
			show_category_add_box(t_value);
			$(".google_category_list_add_button a").hide();
			$(".google_category_save a").hide();
			$(".google_category_list_add_box select").remove();
			return false;
		});

		$(".google_category_list_add_box .add").die();
		$(".google_category_list_add_box .add").live("click", function(){
			var t_already_exists = false;
			$.each($(".google_category_list input"), function(key, value){
				if($(value).val() == $(".google_category_list_add_box .category_string").val()){
					t_already_exists = true;
				}
			});

			if($(".google_category_list_add_box .category_string").val() != "" && t_already_exists == false){
				if($(".google_category_list_add_box").hasClass("edit")){
					$(".google_category_list input.edit").val($(".google_category_list_add_box .category_string").val());
					$(".google_category_list input").removeClass("edit");
				}else{
					var t_input = $("<input></input>").attr("name", "category_list[][0]").attr("type", "text").attr("readonly", "readonly").val($(".google_category_list_add_box .category_string").val());
					var t_edit_button = $("<a></a>").attr("href", "#").addClass("button edit").html($(".text.button_edit").val());
					$(".google_category_list").append(t_input).append(t_edit_button);
					$(".google_category_list div").show();
				}
				$(".google_category_save a").show();
				$(".google_category_list_add_button a").show();
				$(".google_category_list_add_box").hide();
				$(".google_category_list_add_box").removeClass("add edit");
				$(".google_category_list_add_box select").remove();
			}
			return false;
		});

		$(".google_category_list_add_box .cancle").die();
		$(".google_category_list_add_box .cancle").live("click", function(){
			$(".google_category_list_add_button a").show();
			$(".google_category_save a").show();
			$(".google_category_list_add_box").hide();
			$(".google_category_list_add_box").removeClass("add edit"); 
			$(".google_category_list_add_box select").remove();
			$(".google_category_list input").removeClass("edit");
			return false;
		});

		$(".google_category_list_add_box select").die();
		$(".google_category_list_add_box select").live("change", function(){
			var next_elements = $(this).nextAll();
			$.each(next_elements, function(key, value){
				$(value).remove();
			});
			update_category_string();
			if($(this).val() != ""){
				$(".google_category_list_add_box").append(loading_image_box);
				get_select_options($(".google_category_list_add_box .category_string").val());
			}
		});

		return false;
	});
      
	function fillTemplate(){
		$.ajax({
			type: "GET",
			url: "request_port.php?module=GoogleTaxonomy",
			data: {"action": "get_template", "template": settings.template},
			success: function(template){
				if(template.match(/WARNING\(512\)/) || template == ""){
                    $(window).unbind("scroll");
                    $(window).unbind('resize');
                    $('body').css('overflow', 'auto');
					lightbox_package.remove();
					alert("Smarty Error - Unable to find Template");
				}else if(template.match(/t_action_request not found/)){
                    $(window).unbind("scroll");
                    $(window).unbind('resize');
                    $('body').css('overflow', 'auto');
					lightbox_package.remove();
					alert("Warning - Unable to call function");
				}else{
					lightbox_package_loading_image.hide();
					lightbox_package_shadow.show();
					$.tmpl(template).appendTo(lightbox_package);
					initialize();
				}
			},
			error: function(){
                $(window).unbind("scroll");
                $(window).unbind('resize');
                $('body').css('overflow', 'auto');
				lightbox_package.remove();
				alert("Connection Error - Unable to connect to server");
			}
		});
	}
      
	function initialize(){
		lightbox_package_container = $(".lightbox_package_container");
		lightbox_package_wrapper = $(".lightbox_package_wrapper");
		lightbox_package_content = $(".lightbox_package_content");   
		lightbox_package_container.show();
		update_window();
		$(".lightbox_package_close").bind("click", function(){
            $(window).unbind("scroll");
            $(window).unbind('resize');
            $('body').css('overflow', 'auto');
			lightbox_package.remove();
		});
	}
      
	function update_window(){
        $(lightbox_package).css('position', 'absolute');
        $(lightbox_package).css('top', $(window).scrollTop());
        $('body').css('overflow', 'hidden');
        
		window_width = $(window).width();
		window_height = $(window).height();

		if(settings.lightbox_width != "auto" && settings.lightbox_width != ""){
			lightbox_width = settings.lightbox_width;
		}else if(settings.lightbox_width == "auto"){
			lightbox_width = window_width-60;
		}else{
			lightbox_width = lightbox_package_content.width()+40;
			alert(lightbox_package_content.width());
		}

		if(lightbox_width > window_width-60){
			lightbox_width = window_width-60;
		}

		lightbox_package_container.css("left", (window_width-lightbox_width)/2);

		if(settings.lightbox_height != "auto" && settings.lightbox_height != ""){
			lightbox_height = settings.lightbox_height;
		}else if(settings.lightbox_height == "auto"){
			lightbox_height = window_height-60;
		}

		if(lightbox_package_wrapper.height() > window_height-60 || lightbox_package_wrapper.height() < lightbox_package_content.height()+30){
			lightbox_package_wrapper.height(window_height-60);
			lightbox_height = window_height-60;
		}

		lightbox_package.width(window_width);
		lightbox_package.height(window_height);
		lightbox_package_shadow.width(window_width);
		lightbox_package_shadow.height(window_height);
		lightbox_package_container.width(lightbox_width);
		lightbox_package_container.height(lightbox_height);
		lightbox_package_wrapper.height(lightbox_height);
	}

	function show_google_categories(){
		var $t_category_id = $(".google_categories_administration").attr("id").replace("product_id_", "");
		$.ajax({
			type: "GET",
			url: "request_port.php?module=GoogleTaxonomy",
			data: {"action": "get_product_google_category_array", "product_id": $t_category_id},
			success: function(response){
				response = $.parseJSON(response);
				$.each(response, function(key, value){
					$(".google_category_list div").show();
					var t_input = $("<input></input>").attr("name", "category_list[]["+value.v_products_google_categories_id+"]").attr("type", "text").attr("readonly", "readonly").val(value.v_google_category);
					var t_edit_button = $("<a></a>").attr("href", "#").addClass("button edit").html($(".text.button_edit").val());
					$(".google_category_list").append(t_input).append(t_edit_button);
				});
			}
		});
	}

	function update_category_string(){
		$(".google_category_list_add_box .category_string").empty();
		$.each($(".google_category_list_add_box select"), function(key, value){
			if(key != 0){
				if($(value).val() != ""){
					var t_string = $(".google_category_list_add_box .category_string").val()
					t_string = t_string+" > "+$(value).val();
					$(".google_category_list_add_box .category_string").val(t_string);
				}
			}else{
				$(".google_category_list_add_box .category_string").val($(value).val());
			}
		});
	}

	function show_category_add_box(p_string){
		if(p_string == ""){
			$(".google_category_list_add_box").addClass("add");
		}else{
			$(".google_category_list_add_box").addClass("edit");
		}
		$(".google_category_list_add_box .category_string").val(p_string);
		get_select_options("", p_string);

		$(".google_category_list_add_box").show();
	}

	function show_category_select(p_string, options_array){
		if(options_array != false){
			var cat_array = new Array();
			if($.trim($(".google_category_list_add_box .category_string").val()) != ""){
				cat_array = $(".google_category_list_add_box .category_string").val().split(">");
				$.each(cat_array, function(key, value){						
					cat_array[key] = $.trim(value);
				});
			}

			var category_select = $("<select></select>");
			category_select.append($("<option></option>").attr("value", "").html($(".text.select").val()));
			$.each(options_array, function(key, value){
				value = $.trim(value);
				if(value != ""){
					if($.inArray(value, cat_array) != -1 && cat_array.length > 0 && $(".google_category_list_add_box").hasClass("edit")){
						p_string = $.trim(p_string);
						$(".google_category_list_add_box").append(loading_image_box);
						if(p_string != ""){
							get_select_options(p_string + " > " + value);
						}else{
							get_select_options(value);
						}
						category_select.append($("<option></option>").attr("value", value).html(value).attr("selected", "selected"));
					}else{
						category_select.append($("<option></option>").attr("value", value).html(value));
					}
				}
			});
			$(".google_category_list_add_box").append(category_select);
		}
	}

	function get_select_options(p_string){
		$.ajax({
			type: "GET",
			url: "request_port.php?module=GoogleTaxonomy",
			data: {"action": "get_google_categories_array", "parent": p_string},
			success: function(response){
				response = $.parseJSON(response);
				show_category_select(p_string, response);
				$(loading_image_box).remove();
			},
			error: function(){
				$(loading_image_box).remove();
			}
		});

	}
	return false;     
	
	};  
})(jQuery);

$(document).ready(function()
{
    $("a.lightbox_google_admin_categories").lightbox_google_admin_categories();
});